package com.lz.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lz.common.core.domain.entity.SysUser;
import com.lz.common.utils.DateUtils;
import com.lz.common.utils.SecurityUtils;
import com.lz.common.utils.StringUtils;
import com.lz.manage.mapper.FolderInfoMapper;
import com.lz.manage.model.domain.FolderInfo;
import com.lz.manage.model.dto.folderInfo.FolderInfoQuery;
import com.lz.manage.model.vo.folderInfo.FolderInfoVo;
import com.lz.manage.service.IFolderInfoService;
import com.lz.system.service.ISysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 文件夹信息Service业务层处理
 *
 * @author YY
 * @date 2025-07-17
 */
@Service
public class FolderInfoServiceImpl extends ServiceImpl<FolderInfoMapper, FolderInfo> implements IFolderInfoService {
    @Resource
    private FolderInfoMapper folderInfoMapper;

    @Resource
    private ISysUserService userService;

    //region mybatis代码

    /**
     * 查询文件夹信息
     *
     * @param folderId 文件夹信息主键
     * @return 文件夹信息
     */
    @Override
    public FolderInfo selectFolderInfoByFolderId(Long folderId) {
        return folderInfoMapper.selectFolderInfoByFolderId(folderId);
    }

    /**
     * 查询文件夹信息列表
     *
     * @param folderInfo 文件夹信息
     * @return 文件夹信息
     */
    @Override
    public List<FolderInfo> selectFolderInfoList(FolderInfo folderInfo) {
        List<FolderInfo> folderInfos = folderInfoMapper.selectFolderInfoList(folderInfo);
        for (FolderInfo info : folderInfos) {
            //查询用户名称
            SysUser sysUser = userService.selectUserById(info.getUserId());
            if (StringUtils.isNotNull(sysUser) && StringUtils.isNotEmpty(sysUser.getUserName())) {
                info.setUserName(sysUser.getUserName());
            }
        }
        return folderInfos;
    }

    /**
     * 新增文件夹信息
     *
     * @param folderInfo 文件夹信息
     * @return 结果
     */
    @Override
    public int insertFolderInfo(FolderInfo folderInfo) {
        folderInfo.setUserId(SecurityUtils.getUserId());
        folderInfo.setCreateTime(DateUtils.getNowDate());
        return folderInfoMapper.insertFolderInfo(folderInfo);
    }

    /**
     * 修改文件夹信息
     *
     * @param folderInfo 文件夹信息
     * @return 结果
     */
    @Override
    public int updateFolderInfo(FolderInfo folderInfo) {
        folderInfo.setUpdateTime(DateUtils.getNowDate());
        return folderInfoMapper.updateFolderInfo(folderInfo);
    }

    /**
     * 批量删除文件夹信息
     *
     * @param folderIds 需要删除的文件夹信息主键
     * @return 结果
     */
    @Override
    public int deleteFolderInfoByFolderIds(Long[] folderIds) {
        return folderInfoMapper.deleteFolderInfoByFolderIds(folderIds);
    }

    /**
     * 删除文件夹信息信息
     *
     * @param folderId 文件夹信息主键
     * @return 结果
     */
    @Override
    public int deleteFolderInfoByFolderId(Long folderId) {
        return folderInfoMapper.deleteFolderInfoByFolderId(folderId);
    }

    //endregion
    @Override
    public QueryWrapper<FolderInfo> getQueryWrapper(FolderInfoQuery folderInfoQuery) {
        QueryWrapper<FolderInfo> queryWrapper = new QueryWrapper<>();
        //如果不使用params可以删除
        Map<String, Object> params = folderInfoQuery.getParams();
        if (StringUtils.isNull(params)) {
            params = new HashMap<>();
        }
        Long folderId = folderInfoQuery.getFolderId();
        queryWrapper.eq(StringUtils.isNotNull(folderId), "folder_id", folderId);

        String parentId = folderInfoQuery.getParentId();
        queryWrapper.eq(StringUtils.isNotEmpty(parentId), "parent_id", parentId);

        String folderName = folderInfoQuery.getFolderName();
        queryWrapper.like(StringUtils.isNotEmpty(folderName), "folder_name", folderName);

        Date createTime = folderInfoQuery.getCreateTime();
        queryWrapper.between(StringUtils.isNotNull(params.get("beginCreateTime")) && StringUtils.isNotNull(params.get("endCreateTime")), "create_time", params.get("beginCreateTime"), params.get("endCreateTime"));

        Date updateTime = folderInfoQuery.getUpdateTime();
        queryWrapper.between(StringUtils.isNotNull(params.get("beginUpdateTime")) && StringUtils.isNotNull(params.get("endUpdateTime")), "update_time", params.get("beginUpdateTime"), params.get("endUpdateTime"));

        return queryWrapper;
    }

    @Override
    public List<FolderInfoVo> convertVoList(List<FolderInfo> folderInfoList) {
        if (StringUtils.isEmpty(folderInfoList)) {
            return Collections.emptyList();
        }
        return folderInfoList.stream().map(FolderInfoVo::objToVo).collect(Collectors.toList());
    }

}
